<template>
	<view class="content">
		<uni-badge size="normal" :is-dot="true" :text="item.showNumber" absolute="rightTop" type="error"
			v-for="(item,index) of infoList" :key="index" style="width: 740rpx;">
			<view class="notice-view" @click="toDetail(item)">
				<view class="notice-top">
					<view class="notice-title line-1">
						{{item.title}}
					</view>
					<view class="notice-time">
						{{item.time}}
					</view>
				</view>
				<view class="notice-bottom line-1">
					{{item.message}}
				</view>
			</view>
		</uni-badge>

		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
	var that;
	export default {
		data() {
			return {
				scrollTop: 0,
				page: 0,
				totalElements: 0,
				infoList: []
			};
		},
		onShow() {
			this.infoList = []
			this.page = 0
			this.getList()
		},
		onLoad() {
			that = this
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			// 监听页面滚动到底部的事件触发，执行加载更多操作
			this.page++;
			this.getList()
		},
		methods: {
			getList() {
				const maxItems = (that.page - 1) * 20 + 20; // 计算该页最后一项的索引
				if (maxItems >= that.totalElements && that.totalElements !== 0) {
					console.log('no');
					return false; // 如果该页最后一项的索引大于等于总数，则说明该页已经满了，返回 false
				}
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				const userId = uni.getStorageSync('id')
				that.$u.get('/api/wsSceneMessage', {
					page: that.page,
					size: 20,
					userId
				}).then(res => {
					that.totalElements = res.data.totalElements
					const list = res.data.content
					for (const item of list) {
						item.time = item.createTime.slice(0, -9)
						item.showNumber = item.state == 0 ? 10 : 0;
					}
					that.infoList = [...that.infoList, ...list]
				})
			},
			toDetail(e) {
				that.$u.post('/api/wsSceneMessage/udpateStateById', {
					id: e.id
				})
				uni.setStorageSync('notice', JSON.stringify(e))
				uni.navigateTo({
					url: '/pages/index/noticeDetail'
				})
			},
		},
	};
</script>

<style lang="less">
	page {
		background-color: #F8F8F8;
		padding-top: 25rpx;
	}

	.notice-view {
		width: 720rpx;
		background-color: #fff;
		padding: 25rpx;
		margin: 5rpx auto 25rpx auto;
		border-radius: 25rpx;
		box-shadow: 5rpx 8rpx 15rpx #dcdcdc;
		.notice-top {
			display: flex;
			justify-content: space-between;
			align-items: flex-end;

			.notice-title {
				width: 420rpx;
				font-size: 35rpx;
				color: #303133;
			}

			.notice-time {
				display: flex;
				justify-content: flex-end;
				width: 250rpx;
				font-size: 30rpx;
				color: #909399;
			}
		}

		.notice-bottom {
			color: #868e95;
			font-size: 30rpx;
			margin-top: 25rpx;
		}
	}
</style>